<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
<head>
	 <meta charset="UTF-8">
	 <title th:replace="common/base::pagetitle"></title>
	 <link th:replace="common/base::static"/>
</head>
<body>
<div class="layui-form" style="padding: 20px 30px 0 0;">
	 <div class="layui-form-item">
		  <label class="layui-form-label">归属部门</label>
		  <div class="layui-input-inline">
				<input type="text" id="deptId" th:value="${editInfo.deptId}" th:data-name="${editInfo.deptName}" autocomplete="off" class="layui-input">
		  </div>
	 </div>
	 <div class="layui-form-item">
		  <label class="layui-form-label">App Key</label>
		  <div class="layui-input-block">
				<input type="text" name="appKey" th:value="${editInfo.appKey}" lay-verify="required" placeholder="请输入App Key" autocomplete="off" class="layui-input">
		  </div>
	 </div>
	 <div class="layui-form-item">
		  <label class="layui-form-label">角色</label>
		  <div class="layui-input-inline">
				<input type="text" id="roleCodes" th:value="${editInfo.keyRole==null?'':editInfo.keyRole.roleCodes}" th:data-name="${editInfo.keyRole==null?'':editInfo.keyRole.roleNames}" autocomplete="off" class="layui-input">
		  </div>
	 </div>
	 <div class="layui-form-item">
		  <label class="layui-form-label">签名类型</label>
		  <div class="layui-input-inline">
				<select name="signType">
					 <option value="0" th:selected="${editInfo.signType == 0}">请选择</option>
					 <option value="1" th:selected="${editInfo.signType == 1}">RSA2</option>
					 <option value="2" th:selected="${editInfo.signType == 2}">MD5</option>
				</select>
		  </div>
	 </div>
	 <div class="layui-form-item">
		  <label class="layui-form-label">MD5秘钥
		  </label>
		  <div class="layui-input-block">
				<button type="button" class="layui-btn layui-btn-sm" lay-filter="createSecret" id="createSecret">
					 <i class="layui-icon">重新生成</i>
				</button>
		  </div>
	 </div>
	 <div class="layui-form-item">
		  <!--<label class="layui-form-label">加密秘钥-->
		  <!--</label>-->
		  
		  <div class="layui-input-block">
				<input type="text" name="secret" th:value="${editInfo.secret}" placeholder="签名类型为MD5时使用" autocomplete="off" class="layui-input">
		  </div>
	 </div>
	 <div class="layui-form-item">
		  <label class="layui-form-label">开发者公私钥
		  </label>
		  <div class="layui-input-block">
				<button type="button" class="layui-btn layui-btn-sm" id="createIsvKey">
					 <i class="layui-icon">重新生成</i>
				</button>
		  </div>
	 </div>
	 <div class="layui-form-item">
		  <label class="layui-form-label">开发者公钥</label>
		  <div class="layui-input-block">
				<input type="text" style="vertical-align:top;" name="publicKeyIsv" th:value="${editInfo.publicKeyIsv}" lay-verify="required" placeholder="请输入开发者生成的公钥" autocomplete="off" class="layui-textarea">
		  </div>
	 </div>
	 <div class="layui-form-item">
		  <label class="layui-form-label">开发者私钥（交给开发者）</label>
		  <div class="layui-input-block">
				<input type="text" name="privateKeyIsv" th:value="${editInfo.privateKeyIsv}" lay-verify="required" placeholder="请输入开发者生成的私钥（交给开发者）" autocomplete="off" class="layui-textarea">
		  </div>
	 </div>
	 <div class="layui-form-item">
		  <label class="layui-form-label">平台公私钥
		  </label>
		  <div class="layui-input-block">
				<button type="button" class="layui-btn layui-btn-sm" id="createPlatformKey">
					 <i class="layui-icon">重新生成</i>
				</button>
		  </div>
	 </div>
	 <div class="layui-form-item">
		  <label class="layui-form-label">平台公钥（交给开发者）</label>
		  <div class="layui-input-block">
				<input type="text" name="publicKeyPlatform" th:value="${editInfo.publicKeyPlatform}" lay-verify="required" placeholder="请输入平台生成的公钥（交给开发者）" autocomplete="off" class="layui-textarea">
		  </div>
	 </div>
	 <div class="layui-form-item layui-form-text">
		  <label class="layui-form-label">平台私钥</label>
		  <div class="layui-input-block">
				<input type="text" name="privateKeyPlatform" th:value="${editInfo.privateKeyPlatform}" lay-verify="required" placeholder="请输入平台生成的私钥" autocomplete="off" class="layui-textarea">
		  </div>
	 </div>
	 <div class="layui-form-item">
		  <label class="layui-form-label"></label>
		  <div class="layui-input-block">
		  </div>
	 </div>
	 
	 <div class="layui-form-item layui-hide">
		  <input type="button" lay-submit="" lay-filter="kFormSubmit" id="kFormSubmit">
		  <input type="text" name="id" th:value="${editInfo.id}" autocomplete="off" class="layui-input">
	 </div>
</div>

<script th:replace="common/base::context"></script>
<script th:replace="common/base::mainjs"></script>
<script th:src="@{/static/js/select-tree.js}"></script>
<script>
    layui.use(['form', 'laydate'], function () {
        var $ = layui.$, form = layui.form, laydate = layui.laydate;

        var deptId = wlbKit.getUrlParam('deptId');
        if (!deptId) {
            deptId = $('input[name="deptId"]').val();
        }

        // 下拉树
        var deptSelectTreeObj = initSelectTree("deptId", {
            async: {
                enable: true,
                type: req.type.get,
                url: api.sys.deptListTree,
                autoParam: ["id=parentId"],
                otherParam: {},
                dataFilter: function (treeId, parentNode, responseData) {
                    if (!responseData.data) {
                        return null;
                    }
                    var nodes = responseData.data;
                    $.each(nodes, function (i, n) {
                        if (n.type === 0 || n.type === 1) {
                            n.isParent = true;
                        }
                    });
                    return nodes;
                }
            },
            callback: {
                beforeClick: function (treeId, treeNode, clickFlag) {
                    log(treeNode);
                    if (treeNode.level === 0) { // 不可选中
                        return false;
                    }
                }
            }
        });

        var deptNode = deptSelectTreeObj.getNodeByParam("id", 0, null);
        deptSelectTreeObj.expandNode(deptNode);

        // 角色下拉树
        var roleSelectTreeObj = initSelectTree("roleCodes", {
            async: {
                enable: true,
                type: req.type.get,
                url: BASE_WEB + 'devops/isvRole/list/tree',
                autoParam: ["id=parentId"],
                otherParam: {},
                dataFilter: function (treeId, parentNode, responseData) {
                    if (!responseData.data) {
                        return null;
                    }
                    var nodes = responseData.data;
                    $.each(nodes, function (i, n) {
                        //n.checked= true;
                        if (n.type === 0) {
                            n.isParent = true;
                            n.nocheck = true;
                        }
                    });
                    return nodes;
                }
            },
            callback: {},
            check: {
                enable: true,
                chkStyle: "checkbox",
                chkboxType: {"Y": "s", "N": "s"}
            }
        }, [{id: 0, name: "角色列表", isParent: true, nocheck: true}]);

        var roleNode = roleSelectTreeObj.getNodeByParam("id", 0, null);
        roleSelectTreeObj.expandNode(roleNode);

        laydate.render({
            elem: '#gmtCreate' // 指定元素
        });
        laydate.render({
            elem: '#gmtModified' // 指定元素
        });

        //监听提交
        form.on('submit(kFormSubmit)', function (data) {
            var field = data.field; //获取提交的字段
            var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引

            var url = field.id ? 'devops/keys/edit' : 'devops/keys/add';
            //提交 Ajax 成功后，关闭当前弹层并重载表格
            $.ajax({
                type: req.type.post,
                url: BASE_WEB + url,
                data: field,
                async: false,
                success: function (r) {
                    if (r.code === req.status.ok) {
                        // 重载表格
                        parent.layui.table.reload('kContentTable');
                        parent.layer.close(index); //再执行关闭
                    } else {
                        wlbKit.errorMsg(r.msg);
                    }
                }
            });

        });
        //MD5秘钥生成
        $("#createSecret").click(function () {
            $.ajax({
                type: req.type.get,
                url: BASE_WEB + 'devops/keys/createSecret',
                success: function (r) {
                    if (r.code === req.status.ok) {
                        $("[name='secret']").val(r.data);
                    } else {
                        wlbKit.errorMsg(r.msg);
                    }
                }
            });
        });

        //开发者公私钥生成
        $("#createIsvKey").click(function () {
            $("[name='publicKeyIsv']").val('正在重新生成……');
            $("[name='privateKeyIsv']").val('正在重新生成……');
            $.ajax({
                type: req.type.get,
                url: BASE_WEB + 'devops/keys/createRsa',
                success: function (r) {
                    if (r.code === req.status.ok) {
                        $("[name='publicKeyIsv']").val(r.data.publicKey);
                        $("[name='privateKeyIsv']").val(r.data.privateKey);
                    } else {
                        wlbKit.errorMsg(r.msg);
                    }
                }
            });
        });
        //开台公私钥生成
        $("#createPlatformKey").click(function () {
            $("[name='publicKeyPlatform']").val('正在重新生成……');
            $("[name='privateKeyPlatform']").val('正在重新生成……');
            $.ajax({
                type: req.type.get,
                url: BASE_WEB + 'devops/keys/createRsa',
                success: function (r) {
                    if (r.code === req.status.ok) {
                        $("[name='publicKeyPlatform']").val(r.data.publicKey);
                        $("[name='privateKeyPlatform']").val(r.data.privateKey);
                    } else {
                        wlbKit.errorMsg(r.msg);
                    }
                }
            });
        });
    })
</script>
</body>
</html>